﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace h6
{

    public partial class Form1 : Form
    {
        private readonly HttpClient _httpClient = new HttpClient();
        public Form1()
        {
            InitializeComponent();
            // 事件处理
            btnFetch.Click += async (sender, e) =>
            {
                lstPhones.Items.Clear();
                lstEmails.Items.Clear();

                if (string.IsNullOrWhiteSpace(txtUrl.Text))
                {
                    MessageBox.Show("请输入有效的URL");
                    return;
                }

                try
                {
                    string html = await _httpClient.GetStringAsync(txtUrl.Text);
                    ExtractAndDisplayInfo(html, lstPhones, lstEmails);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"错误: {ex.Message}");
                }
            };
        }
        private void ExtractAndDisplayInfo(string html, ListBox phoneList, ListBox emailList)
        {
            // 手机号码正则表达式（匹配11位中国大陆手机号）
            string phonePattern = @"(?<!\d)(1[3-9]\d{9})(?!\d)";
            // 邮箱地址正则表达式
            string emailPattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b";

            var phones = new HashSet<string>();
            var emails = new HashSet<string>();

            // 查找手机号码
            foreach (Match match in Regex.Matches(html, phonePattern))
            {
                phones.Add(match.Groups[1].Value);
            }

            // 查找邮箱地址
            foreach (Match match in Regex.Matches(html, emailPattern, RegexOptions.IgnoreCase))
            {
                emails.Add(match.Value);
            }

            // 更新UI
            phoneList.BeginUpdate();
            phoneList.Items.AddRange(phones.ToArray());
            phoneList.EndUpdate();

            emailList.BeginUpdate();
            emailList.Items.AddRange(emails.ToArray());
            emailList.EndUpdate();
        }
    }
}
